package com.ftg.learn.bus.service.impl;

import com.ftg.learn.bus.config.WebLogAspect;
import com.ftg.learn.bus.entity.LoginUser;
import com.ftg.learn.bus.lnterceptor.LoginInterceptor;
import com.ftg.learn.bus.service.ILogService;
import com.ftg.learn.bus.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author Guo
 * @date 2022/3/13
 */
@Service
public class shiwuServiceImpl {

    @Autowired
    private ILogService iLogService;


    public final static Map<String, String> MAP = new HashMap<String, String>(5);

    static {
        MAP.put("/bus/user/roleGroupOne", "在角色功能模块进行修改操作");
    }

    /**
     * 切面添加日志操作，事务Transactional 需要单独提出来
     * @param servletPath
     */
//    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void shiwu(String servletPath) {
        //自定义判断操作路径不是MAP里的不处理
        //获取请求地址
        int index = servletPath.lastIndexOf("/");

        String newDel = servletPath.substring(0,index);
        int newIndex = newDel.lastIndexOf("/");

        int index1 = servletPath.indexOf("/");
        String str = servletPath.substring(index1+1);
        System.out.println("index1================" + index1);
        System.out.println("str================" + str);

        int index2 = str.indexOf("/");
        String str2 = str.substring(index2+1);
        System.out.println("index2================" + index2);
        System.out.println("str2================" + str2);

        int index3 = str2.indexOf("/");
        String str1 = str2.substring(0,index3);
        System.out.println("index3===========================>"+index3);
        System.out.println("str1===========================>"+str1);

        System.out.println("newDel===========================>"+newDel);
        System.out.println("str1===========================>"+str1);
        switch (str1){
            case "user":
                str1 = "用户";
                break;
            case "role":
                str1 = "角色";
                break;
            case "menu":
                str1 = "菜单";
                break;
            case "bus-info":
                str1 = "公车信息";
                break;
            case "bus-use-registration":
                str1 = "使用登记";
                break;
            case "bus-expenditure":
                str1 = "经费支出登记";
                break;
            case "approval" :
                str1 = "审批";
                break;
        }
        if (servletPath.substring(index+1).length() >= 6){
            if ("update".equals(servletPath.substring(index+1,index+7))){
                MAP.put(servletPath,"在"+str1+"功能模块内进行了修改操作");
            }else if ("insert".equals(servletPath.substring(index+1,index+7))){
                MAP.put(servletPath,"在"+str1+"功能模块内进行了添加操作");
            }
        }else if (servletPath.substring(index+1).length() >= 3){
            if ("save".equals(servletPath.substring(index+1,index+5))){
                MAP.put(servletPath,"在"+str1+"功能模块内进行了添加操作");
            }
        }else if (newDel.substring(newIndex+1).length() >= 3){
            if ("del".equals(newDel.substring(newIndex+1,newIndex+4))){
                MAP.put(servletPath,"在"+str1+"功能模块内进行了删除操作");
            }
        }

        String gatPath = MAP.get(servletPath);
        //判断此请求地址存在与MAP中
        if (Objects.nonNull(gatPath)) {
            // 拿到这个登录人的 信息。
            LoginUser loginUser = LoginInterceptor.loginUserThreadLocal.get();
            Long id = loginUser.getId();
            String name = loginUser.getName();
            iLogService.addLog(gatPath,servletPath,id,name);
//            int a = 1 / 0;
//            return false;
        }
    }
}
